package com.sunday.android.fangdaifu.bukong;

import android.app.Application;
import android.os.Build;
import android.os.StrictMode;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.sunday.android.fangdaifu.bukong.utils.Constants;
import com.sunday.common.logger.Logger;
import com.sunday.member.common.MemberConst;
import com.sunday.member.utils.SharePerferenceUtils;
import com.umeng.analytics.MobclickAgent;


/**
 * Created by Administrator on 2016/2/23.
 */
public class BaseApp extends Application implements Thread.UncaughtExceptionHandler {


    private static BaseApp instance;
    public static boolean isFinish = true;
    private String tempcoor = "bd09ll";

    public LocationClient mLocationClient;
    private MyLocationListener mMyLocationListener;
    private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;

    public synchronized static BaseApp getInstance() {
        return instance;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.detectFileUriExposure();
        }
        //初始化地图 SDK
        SDKInitializer.initialize(getApplicationContext());
        mLocationClient = new LocationClient(this.getApplicationContext());
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
        initLocation();
        Logger.init("Sunday").hideThreadInfo().setMethodCount(3).setMethodOffset(2);
        Thread.setDefaultUncaughtExceptionHandler(this);


    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(tempMode);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType(tempcoor);//可选，默认gcj02，设置返回的定位结果坐标系，
        int span = 1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        mLocationClient.setLocOption(option);
    }

    /**
     * 实现实时位置回调监听
     */
    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location.getLatitude() != 0) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LATITUDE, String.valueOf(location.getLatitude()));
            }
            if (location.getLongitude() != 0) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LONGITUDE, String.valueOf(location.getLongitude()));
            }
            if (!TextUtils.isEmpty(location.getAddrStr())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCATION_ADDRESS, location.getAddrStr());
            }
            if (!TextUtils.isEmpty(location.getProvince())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCATION_PROVINCE, location.getProvince());
            }
            if (!TextUtils.isEmpty(location.getCity())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCALTION_CITY, String.valueOf(location.getCity()));
            }
            if (!TextUtils.isEmpty(location.getDistrict())) {
                SharePerferenceUtils.getIns(getApplicationContext()).putString(Constants.LOCALTION_DISTRCT, String.valueOf(location.getDistrict()));
            }
            mLocationClient.stop();
            if(listenr != null){
                listenr.finish(location);
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }

    }

    private LocationListenr listenr;

    public interface LocationListenr{
        public void finish(BDLocation location);
    }

    public void setListenr(LocationListenr listenr) {
        this.listenr = listenr;
    }



    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        if(!BuildConfig.DEBUG){
            MobclickAgent.reportError(this, ex);
            System.exit(0);
        }

    }


    public long getMemberId() {
        long memberId = SharePerferenceUtils.getIns(this).getLong(MemberConst.MEMBER_ID, -1);
        return memberId;
    }

    public void setMemberId(long memberId) {
        SharePerferenceUtils.getIns(this).putLong(MemberConst.MEMBER_ID, memberId);
    }
}
